/**
 * role list
 */
layui.use(['form', 'table', 'tableSelect'], function () {
    var $ = layui.jquery,
        form = layui.form,
        tableSelect = layui.tableSelect,
        table = layui.table;
    let searchFieldsetStatus=localStorage.getItem("searchFieldsetStatus");
    if(searchFieldsetStatus=='0')
    {
        $(".search-display-btn").attr("show","0");
        $(".search-display-btn").html('<i class="fa fa-angle-down"></i>展开');
        $($('.layui-form')[0]).hide();
    }
    setTableHeight();
    let u = localStorage.getItem("ADMIN_LOGIN");
    let loginU = {};
    if (u == undefined) {
        layer.msg('请登录', {icon: 1, time: 500}, function () {
            window.location = 'logout';
        });
    } else {
        loginU = decryptLocalStorage(u);
        if (loginU.userType != '1') {
            // $("#shipName").attr("disabled","disabled");
        } else {

        }
        // $("#shipId").val(loginU.shipId==0?"":loginU.shipId);
        // $("#shipName").val(loginU.shipName);
    }
    let shipId = "";
    let shipName = "";
    // let keyWordShipId=localStorage.getItem("keyWordShipId");
    // if(keyWordShipId!=undefined && keyWordShipId!='undefined'){shipId=keyWordShipId;$("#shipId").val(shipId);}
    // let keyWordShipName=localStorage.getItem("keyWordShipName");
    // if(keyWordShipName!=undefined && keyWordShipName!='undefined'){shipName=keyWordShipName;$("#shipName").val(shipName);}
    loadShip();
    // if(loginU.shipId!=0){
    //     shipId=loginU.shipId
    // }
    let tableUrl = baseUrl + "admin/v1/role/page?token=" + loginU.token;
    table.render({
        elem: '#currentTableId',
        url: tableUrl,loginUserType:loginU.userType,
        where: {
            keyWork: JSON.stringify({shipId: shipId,shipName:shipName})
        },
        toolbar: '#toolbarPanel',
        defaultToolbar: ['filter', 'exports'],
        cols: [[
            {type: "checkbox", align: "center", width: 50},
            {type: "numbers", title: '序号', width: 60, align: "center"},
            {field: 'systemType', align:"center", title: '系统名称', sort: true,
                templet: function (data) {
                    if (data.systemType =='ALL') {
                        return "全系统";
                    }else if (data.systemType =='PMS') {
                        return "保养系统";
                    } else if (data.systemType =='VSATS') {
                        return "VSAT系统";
                    }else if (data.systemType =='RCS') {
                        return "充值系统";
                    }else {
                        return "其他"
                    }
                }
                },
            {field: 'roleName', align: "center", title: '角色名称', sort: true},
            {
                field: 'roleMenuCount', align: "center", title: '是否分配菜单', sort: true,
                templet: function (data) {
                    if (data.roleMenuCount > 0) {
                        return "是";
                    } else {
                        return "否"
                    }
                }
            },
            {fixed: "right",title: '操作', minWidth: 220, toolbar: '#currentTableBar', align: "center"}
        ]],
        limits: [10, 15, 20, 25, 50, 100, 200, 300, 500],
        limit: 20,
        page: true,
        skin: 'line',parseData: function (res) {
            for (let i in res.data) {
                res.data[i].loginUserType = loginU.userType;
            }
            return res;
        },
        done: function (res, curr, count) {
            if (res.code === -1) {
                layer.msg('请登录', {icon: 1, time: 500}, function () {
                    window.location = 'logout';
                });
                return;
            }
        }
    });

    // 监听搜索操作
    let keyWork = "{}";
    form.on('submit(data-search-btn)', function (data) {
        // let keyWordShipId=data.field.shipId;
        // let keyWordShipName=data.field.shipName;
        // if(keyWordShipId!=undefined && keyWordShipId!='undefined'){localStorage.setItem("keyWordShipId",keyWordShipId);}
        // if(keyWordShipName!=undefined && keyWordShipName!='undefined'){localStorage.setItem("keyWordShipName", keyWordShipName);}
        keyWork = JSON.stringify(data.field);
        setTableHeight();
        //执行搜索重载
        table.reload('currentTableId', {
            page: {
                curr: 1
            }
            , where: {
                keyWork: keyWork
            }
        }, 'data');

        return false;
    });

    /**
     * toolbar监听事件
     */
    table.on('toolbar(currentTableFilter)', function (obj) {
        if (obj.event === 'add') {  // 监听添加操作
            localStorage.removeItem('roleData');
            var index = layer.open({
                title: '添加角色',
                type: 2,
                shade: 0.2,
                maxmin: true,
                shadeClose: false,
                area: ['80%', '80%'],
                content: '../page/role-menu/edit',
                end: function () { setTableHeight();
                    //执行搜索重载
                    table.reload('currentTableId', {
                        page: {
                            curr: 1
                        }
                        , where: {
                            keyWork: keyWork
                        }
                    }, 'data');
                }

            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        } else if (obj.event === 'delete') {  // 监听删除操作
            var checkStatus = table.checkStatus('currentTableId')
                , data = checkStatus.data;
            let params = {token: loginU.token, ids: ''};
            let ids = '';
            if (data.length > 0) {
                for (let i = 0; i < data.length; i++) {
                    if (data[i].roleName == '超级管理员') {
                        layer.msg('超级管理员角色不能删除', {
                            icon: 2,
                            time: 2000
                        });
                        return;
                    }
                    ids += data[i].roleId + ',';
                }
                ids = ids.substring(0, ids.length - 1);
            }
            params.ids = ids;


            layer.confirm("确认删除角色，请谨慎操作？", {
                btn: ["确定", "取消"]
            }, function (index) {
                layer.close(index);
                request.delList('role', params, function (result) {
                    if (result.code == 0) {
                        layer.msg('删除成功', {
                            icon: 1,
                            time: 500
                        }, function () {
                            //执行搜索重载
                            setTableHeight();
                            table.reload('currentTableId', {
                                page: {
                                    curr: 1
                                }
                                , where: {
                                    keyWork: keyWork
                                }
                            }, 'data');
                        });
                    } else {
                        layer.msg(result.msg || '删除失败', {
                            icon: 2,
                            time: 2000
                        });
                    }
                });
            }, function () {
                layer.close(index);
            });


        }
    });

    //监听表格复选框选择
    table.on('checkbox(currentTableFilter)', function (obj) {
        //console.log(obj)
    });

    table.on('tool(currentTableFilter)', function (obj) {
        var data = obj.data;
        if (obj.event === 'menu') {
            console.log(JSON.stringify(data));
            localStorage.setItem("roleData", encryptLocalStorage(data, loginU.token));
            var index = layer.open({
                title: '编辑角色',
                type: 2,
                shade: 0.2,
                maxmin: true,
                shadeClose: false,
                area: ['80%', '80%'],
                content: '../page/role-menu/edit',
                end: function () { setTableHeight();
                    //执行搜索重载
                    table.reload('currentTableId', {
                        page: {
                            curr: 1
                        }
                        , where: {
                            keyWork: keyWork
                        }
                    }, 'data');
                }

            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        } else if (obj.event === 'edit') {
            localStorage.setItem("roleData", encryptLocalStorage(data, loginU.token));
            var index = layer.open({
                title: '编辑角色',
                type: 2,
                shade: 0.2,
                maxmin: true,
                shadeClose: false,
                area: ['80%', '80%'],
                content: '../page/role/edit',
                end: function () { setTableHeight();
                    //执行搜索重载
                    table.reload('currentTableId', {
                        page: {
                            curr: 1
                        }
                        , where: {
                            keyWork: keyWork
                        }
                    }, 'data');
                }

            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        } else if (obj.event === 'delete') {
            if (data.roleName == '超级管理员') {
                layer.msg('超级管理员角色不能删除', {
                    icon: 2,
                    time: 2000
                });
                return;
            }
            layer.confirm("确认删除角色，请谨慎操作？", {
                btn: ["确定", "取消"]
            }, function (index) {
                layer.close(index);
                request.del('role', {token: loginU.token, id: obj.data.roleId}, function (result) {
                    if (result.code == 0) {
                        layer.msg('删除成功', {
                            icon: 1,
                            time: 500
                        }, function () {
                            obj.del();
                        });
                    } else {
                        layer.msg(result.msg || '删除失败', {
                            icon: 2,
                            time: 2000
                        });
                    }
                });
            }, function () {
                layer.close(index);
            });


        }
    });

    function loadShip() {
        let tableUrl = "/admin/v1/ship/page?token=" + loginU.token;
        tableSelect.render({
            elem: '#shipName',
            checkedKey: 'id',
            searchFlag: true,
            searchKey: 'shipName',
            table: {
                url: tableUrl,loginUserType:loginU.userType,
                cols: [[
                    {type: 'radio'},
                    {field: 'shipId', title: 'ID'},
                    {field: 'shipName', title: '船舶'}
                ]]
            },
            done: function (elem, data) {
                if (data.data.length > 0) {
                    shipId = data.data[0].shipId;
                    $("#shipId").val(data.data[0].shipId);
                    $("#shipName").val(data.data[0].shipName);
                } else {
                    shipId = "";
                    $("#shipId").val("");
                    $("#shipName").val("");
                }

            },
            clear: function () {
                shipId = "";
                $("#shipId").val("");
                $("#shipName").val("");

            }
        })
    }

});
